local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local Game_Cfg=_G.CommonOHOH.config.Game


local function_list = ServerOHOH.function_list;
local PetTool=_G.CommonOHOH.load.PetTool();
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local ConnectClass=_G.CommonOHOH.load.Connect();


function MergeOne(key,hadList,equipList)
    local cfg=PetTool.GetPetDataByPetId(key)
    if not hadList[key] then
      return false
    end
    local need= cfg.merge
    if need==0 then
        return false
    end

    if hadList[key]<need then
        return false
    end
    local addCount=math.floor(hadList[key]/need)
    hadList[key]=hadList[key]-  addCount*need
    local newKey=PetTool.GetUpLevelPetIdByPetId(key)
    if not hadList[newKey] then
        hadList[newKey]=addCount
    else
        hadList[newKey]= hadList[newKey]+addCount
    end
    PetTool.UpdateEquip(key,hadList,equipList)
    return true
end


function M.new(self)
	return M(self);
end;

--获取宠物加成值
function M.GetPetBoost(self)
    local sum=0
    local OnwedPet=self.Database:GetData("pets").had.__real

    for k,v in pairs(self.Database:GetData("pets").equip.__real) do
       sum=sum+PetTool.GetPetBoostValueByPetId(k,OnwedPet)*v
    end
    return sum
end

function M.ctor(self, parent)
	M.super.ctor(self, parent)
    self.player=self.parent
    self.Database= self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());

    --宠物对经验加成
    ServerOHOH.load.CallBackManager():AddRbxConnect(self,self.player.ExpValueMulEvent, function()
        return self:GetPetBoost()
    end);

	self.PetEuqipUpdateEvent=self.player.PetEuqipUpdateEvent
	self.PetCreateEvent=self.player.PetRandomCreateEvent
    self.HatchEggEvent=self.player.HatchEggEvent


    if self.player.character then
        self.player.character:AddPrivateComponent(script.PetView)
    end

    self.player.CharacterEnterEvent:Connect(function(character)
            character:AddPrivateComponent(script.PetView)
    end)

    self.player:AddPrivateComponentByFolder(script.PrivateCtrl)


end;

function M.GetEquipCount(self)
    local count=0
    for k,v in pairs(self.Database:GetData("pets").equip.__real) do
        count=count+v
    end
    return count
end

function M.GetPackCount(self)
    local count=0
    for k,v in pairs(self.Database:GetData("pets").had.__real) do
        count=count+v
    end
    return count
end

-- 摇付费蛋
function M.HatchEgg(self,itemId,count)
    local key,id= _G.CommonOHOH.load.ItemTool().GetKeyAndID(itemId)
    self.player:GetPrivateComponent("HatchEgg"):BaseHatch(id,count)
    self.parent:SynchDataToClient("pets")
end


function M.dtor(self)
	M.super.dtor(self)
end;

--------------------------客户端调用接口----------------------------------------
--合成一个
function M.MergeOne(self,petId)
    local petsData=self.Database:GetData("pets")
    MergeOne(petId,petsData.had,petsData.equip)

    self.Database:Execut(function()
	end);
    self.parent:SynchDataToClient("pets")

    self.PetEuqipUpdateEvent:Action()

    return true
end

--合成所有
function M.MergeAll(self)
    local mergList={}
    local petsData=self.Database:GetData("pets")

    for key,v in pairs(petsData.had.__real) do
        local cfg= PetTool.GetPetDataByPetId(key)
       
        table.insert(mergList,cfg)
    end

    table.sort(mergList,function(a,b)
        return a.value<b.value
    end)

    for _,v in pairs(mergList) do
        MergeOne(v.petId,petsData.had,petsData.equip)
    end

    self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")
    self.PetEuqipUpdateEvent:Action()
    return true
end

--删除一组数据
function M.Delete(self,list)
    local petsData=self.Database:GetData("pets")
    for _,key in pairs(list) do
        if not petsData.had[key] then
            continue
        end
        if petsData.had[key]>0 then
            petsData.had[key]=petsData.had[key]-1
        end
        PetTool.UpdateEquip(key,petsData.had,petsData.equip)
    end  

    self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")

    self.PetEuqipUpdateEvent:Action()

    return true
end

--装备最佳
function M.EquipBest(self)

    local maxEquip=self.Database:GetData("petsPack").equipMax
    local petsData=self.Database:GetData("pets")


    local equipList={}
    for key,v in pairs(petsData.had.__real) do
        local val=PetTool.GetPetBoostValueByPetId(key,petsData.had.__real)

        table.insert(equipList,{
            ["petId"]=key,
            ["value"]=val,
        })
    end

    table.sort(equipList,function(a,b)
        return a.value>b.value
    end)
    petsData.equip={}
    for _,v in pairs(equipList) do
        if maxEquip==0 then
            break
        end
        local key=v.petId
        if petsData.had[key]>maxEquip then
            petsData.equip[key]=maxEquip
            maxEquip=0
            break
        else
            petsData.equip[key]=petsData.had[key]
            maxEquip=maxEquip-petsData.had[key]
        end
    end

    self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")

    self.PetEuqipUpdateEvent:Action()

    return true
end;

--卸载所有
function M.UnEquipAll(self)
    local petsData=self.Database:GetData("pets")
    petsData.equip={}
	self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")

    self.PetEuqipUpdateEvent:Action()

    return true
end;


--装备一组数据
function M.Equip(self,list)
    local petsData=self.Database:GetData("pets")
    local hadEquip=self:GetEquipCount()
    local maxEquip=self.Database:GetData("petsPack").equipMax
    if hadEquip>=maxEquip then
        return false
    end

    for _,key in pairs(list) do
        if petsData.had.__real[key] and petsData.had.__real[key]>0 then
            local eq=0
            if petsData.equip.__real[key] then
                eq=petsData.equip.__real[key]
            end
            eq=eq+1
            hadEquip=hadEquip+1

            if hadEquip>maxEquip then

                self.Database:Execut(function()
                end);
                self.parent:SynchDataToClient("pets")
                self.PetEuqipUpdateEvent:Action()

                return false
            end

            if petsData.had.__real[key]-eq>=0 then
                petsData.equip[key]=eq
            end
        end
    end

    self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")
    self.PetEuqipUpdateEvent:Action()
    return true
end

--卸载一组数据
function M.UnEquip(self,list)
    local petsData=self.Database:GetData("pets")
    for _,key in pairs(list) do
        if petsData.equip.__real[key] and petsData.equip.__real[key]>0 then
            petsData.equip[key]=petsData.equip[key]-1
            if petsData.equip[key]==0 then
                petsData.equip[key]=nil
            end
        end
    end

    self.Database:Execut(function()
	end);
	self.parent:SynchDataToClient("pets")
    self.PetEuqipUpdateEvent:Action()
    return true
end




M:RegistFunction(function_list.Pets_Equip,M.Equip)
M:RegistFunction(function_list.Pets_UnEquip,M.UnEquip)
M:RegistFunction(function_list.Pets_UnEquipAll,M.UnEquipAll)
M:RegistFunction(function_list.Pets_EquipBest,M.EquipBest)
M:RegistFunction(function_list.Pets_Delete,M.Delete)
M:RegistFunction(function_list.Pets_MergeAll,M.MergeAll)
M:RegistFunction(function_list.Pets_MergeOne,M.MergeOne)



return M